<?php
/**
 * Upgrade from 1.2.0 to 1.2.1
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: Upgrade.php 467 2011-01-25 17:24:30Z steen.rabol $
 */

Prado::using('Application.Common.Upgrade.PortalUpgrade');
class Upgrade_1_2_0 extends PortalUpgrade
{
	public function run()
	{
		parent::run();

		if(!$this->UpdateTables())
		{
			PortalUtil::fatalError("Upgrade faild: Update tables");
			return;
		}

		if(!$this->CreateNewTables())
		{
			PortalUtil::fatalError("Upgrade faild: Unable to create new tables");
			return;
		}

		if(!$this->DataUpgrade())
		{
			PortalUtil::fatalError("Upgrade faild: Data upgrade");
			return;
		}

		// Create the Portles admin directory
		$padmDir	= Prado::getPathOfNamespace('Application.Pages.Admin.Portlets');
		PortalUtil::CreateDirStructure($padmDir);

		$this->UpdVersionNum('1.2.1');
	}

	public function UpdateTables()
	{
        $sql = array(	"ALTER TABLE `tblpagecontent` ADD  `culture` VARCHAR( 25 ) NOT NULL",
						"ALTER TABLE `tblhtmlportlet` ADD `culture` VARCHAR( 25 ) NOT NULL ",
						"ALTER TABLE `tblsimpletext` ADD `culture` VARCHAR( 25 ) NOT NULL ",
						"ALTER TABLE `tblnews` ADD `culture` VARCHAR( 25 ) NOT NULL ");

		try
		{
			if(is_array($sql) && count($sql) != 0)
			{
				foreach($sql as $s)
				{
					try
					{
						$this->DbConnection->createCommand($s)->execute();
					}
					catch (TDbException $dbe)
					{
						PortalUtil::Log(sprintf("DB: %d %s, %s", $dbe->ErrorCode, $dbe->ErrorMessage,$s));
					}
				}
			}
		}
		catch(Exception $e)
		{
			PortalUtil::fatalError("Update tables:<br>" . $e->getMessage());
		}
		return true;
	}

	public function CreateNewTables()
	{
		$sql = array();
		try
		{
			if(is_array($sql) && count($sql) != 0)
			{
				foreach($sql as $s)
				{
					$this->DbConnection->createCommand($s)->execute();
				}
			}
		}
		catch(Exception $e)
		{
			PortalUtil::fatalError("Create tables:<br>" . $e->getMessage());
			return false;
		}

		return true;
	}

	public function DataUpgrade()
	{
        // Set culture on page contents
        $culture = $this->Application->Culture;
        $sql = array("update tblpagecontent set culture='$culture' where culture=''");

		try
		{
			if(is_array($sql) && count($sql) != 0)
			{
				foreach($sql as $s)
				{
					$this->DbConnection->createCommand($s)->execute();
				}
			}
		}
		catch(Exception $e)
		{
			PortalUtil::fatalError("Create tables:<br>" . $e->getMessage());
			return false;
		}

		return true;
	}
}
?>